export const chooseAddress = ()=>{
    return new Promise((resolve, reject)=>{
        wx.chooseAddress({
            success: (result) => {
                resolve(result);
            },
            fail: (error) => {
                reject(error)
            },
        });
    })
}

export const showToast = ({title})=>{
    return new Promise((resolve, reject)=>{
        wx.showToast({
          title: title,
          icon:'none',
          success:(res)=>{res},
          fail:(err)=>{err}
        })
    })
}

export const login = ()=>{
    return new Promise((resolve, reject)=>{
        wx.login({
          success: (res) => {resolve(res)},
          fail:(err)=>{reject(err)}
        })
    })
}

export const getUserProfile = ()=>{
    return new Promise((resolve, reject)=>{
        wx.getUserProfile({
            desc: '用于授权',
            success:(res)=>{
                // resolve(res);
                const token = "Bearer\ eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo";
                wx.setStorageSync('token', token);
                wx.navigateBack({
                  delta: 1
                });
                resolve(res);
            },
            fail:(res)=>{
                // const token = "";
                // wx.setStorageSync('token', token);
                
                wx.showToast({
                    title: '授权失败',
                    mask: true,
                    icon: "none"
                });
                setTimeout(() => {
                    wx.switchTab({
                        url: '/pages/user/index',
                    })
                }, 1000);
                reject(res);
            }
        })
    })
}